﻿using System;
using System.Data;
using System.Configuration; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
using System.Data.SqlClient;
using System.IO;
/// <summary>
/// Summary description for BusinessLogic
/// </summary>
public class BusinessLogic
{
    
    DataSet ds = null;
    DataTable dtTemp = null;
    SendEmails auto = new SendEmails();
    RegistrationData regdata = new RegistrationData();
    int UserID = 0, IsPaid = 0, UserTypeID = 0, IsAccountActive = 0;
    string UserType = string.Empty;

    public void InsertSession(DataSet ds, bool chkRemember)
    {
        DataRow dr = ds.Tables[0].Rows[0];
        if (dr["UserID"] != DBNull.Value)
        {
            regdata.UserID = Convert.ToInt32(dr["UserID"].ToString());
        }
        if (dr["Email"] != DBNull.Value)
        {
            regdata.Email = dr["Email"].ToString();
        }
        if (dr["FirstName"] != DBNull.Value)
        {
            regdata.FirstName = dr["FirstName"].ToString();
        }
        if (dr["LastName"] != DBNull.Value)
        {
            regdata.LastName = dr["LastName"].ToString();
        }
        if (dr["UserName"] != DBNull.Value)
        {
            regdata.UserName = dr["UserName"].ToString();
        }
        //if (dr["UserTypeID"] != DBNull.Value)
        //{
        //    regdata.UserTypeID = Convert.ToInt32(dr["UserTypeID"].ToString());
        //}
        
        //if(ds.Tables.Count>1)
        //{
        //    if (ds.Tables[1].Rows.Count > 0)
        //    {
        //        regdata.IsPaid = 1;
        //        regdata.IsAccountActive = Convert.ToInt32(ds.Tables[1].Rows[0]["IsAccountActive"]);
        //    }
        //}
        if (chkRemember)
        {
            HttpContext.Current.Response.Cookies["User"]["UserID"] = regdata.UserID.ToString();
            HttpContext.Current.Response.Cookies["User"].Expires = DateTime.Now.AddMonths(1);
        }
        HttpContext.Current.Session["User"] = regdata; 
    }

    public void checkcookies_ForLogin( string url)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["User"];
        if (cookie != null)
        {
            if (cookie["UserID"] != null)
            {
                UserID = Convert.ToInt32(cookie["UserID"]);
                DataSet registration = new DataSet();
                registration = regdata.GetUserID(UserID);
                if (registration != null)
                {
                    if (registration.Tables.Count > 0)
                    {
                        if (registration.Tables[0].Rows.Count > 0)
                        {
                            InsertSession(registration, false);
                            HttpContext.Current.Response.Redirect(url);
                        }
                    }
                }
            }
            //else
            //    HttpContext.Current.Response.Redirect(Common.LogOutPage);
        }
        //else
        //    HttpContext.Current.Response.Redirect(Common.LogOutPage);

    }

    public void LogOut()
    {
        HttpContext.Current.Session.Abandon();
        if (HttpContext.Current.Response.Cookies["User"] != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["User"];
            cookie.Expires = DateTime.Now.AddDays(-1);
        }
        HttpContext.Current.Response.Redirect(Common.LogOutPage);

           //HttpContext.Current.Response.Redirect(regdata.DefaultRedirect);
    }

    public void CheckAd_IsPostedByUser(int UserID, int AdID)
    {

    }

    int num = 0,i = 1;
    string ext = string.Empty;
    public string ValidateImageName(string path, string name)
    {
        string pathname = path + name;
        pathname = pathname.Replace("~/", "");
        if (File.Exists(HttpContext.Current.Server.MapPath(pathname)))
        {
            num = name.LastIndexOf(".");
            ext = name.Substring(num, name.Length - num);
            name = name.Substring(0, num);
            if (i > 1 && i <= 10)
            {
                name = name.Remove(name.Length - 1);
            }
            else if (i > 10)
            {
                String str = "" + i.ToString();
                char[] digit = str.ToCharArray();
                name = name.Remove(name.Length - digit.Length);
            }
            name = name + i;
            name = name + ext;
            i++;
            name = ValidateImageName(path, name);
            return name;
        }
        return name;
    }


  
}